package date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalField;

public class DateTest {

    public static void main(String[] args) {
        //获取当前日期
        LocalDate date = LocalDate.now();
        System.out.println("今天是 ： "+date);
        //获取年月日
        int year = date.getYear();
        int monthValue = date.getMonthValue();
        int dayOfMonth = date.getDayOfMonth();
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        System.out.printf("Year : %d  Month : %d  day : %d t  week: %d", year, monthValue, dayOfMonth , dayOfWeek.getValue());
        //处理特定日期
        LocalDate dateOfBirth = LocalDate.of(2018, 01, 21);
        System.out.println("特定日期是:"+dateOfBirth); // 2018-01-21
        dateOfBirth = LocalDate.of(1900,01,01);
        System.out.println(dateOfBirth); //1900-01-01

        //处理周期性日期
        LocalDate today = LocalDate.now();
        //LocalDate dateOfBirth = LocalDate.of(2018, 01, 21);

        MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(today);

        System.out.println("birthday:"+birthday+",currentMonthDay:"+currentMonthDay);
        if(currentMonthDay.equals(birthday)){
            System.out.println("Many Many happy returns of the day !!");
        }else{
            System.out.println("Sorry, today is not your birthday");
        }

        //获取当前时间
        LocalTime now = LocalTime.now();
        System.out.println("local time now: " +now);

        //现有时间增加两小时
        LocalTime newTime = now.plusHours(2);
        System.out.println("Time after 2 hours :"+newTime);

        //如何计算一个星期之后的日期
        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);    //使用变量赋值
        System.out.println("Today is : " + today);
        System.out.println("Date after 1 week : " + nextWeek);

        //计算一年前或一年后的日期
        LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println("Date before 1 year : " + previousYear);

        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("Date after 1 year : " + nextYear);

        //时钟类
        // 根据系统时间返回当前时间并设置为UTC。
        Clock clock = Clock.systemUTC();
        System.out.println("Clock : " + clock.instant());

        // 根据系统时钟区域返回时间
        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("Clock : " + clock.instant());

        //如何用Java判断日期是早于还是晚于另一个日期
        LocalDate tomorrow = LocalDate.of(2018, 1, 29);
        if(tomorrow.isAfter(today)){
            System.out.println("Tomorrow comes after today");
        }

        //减去一天
        LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);

        if(yesterday.isBefore(today)){
            System.out.println("Yesterday is day before today");
        }

        //获取特定时区下面的时间
        //设置时区
        ZoneId america = ZoneId.of("Indian/Chagos");

        LocalDateTime localtDateAndTime = LocalDateTime.now();

        ZonedDateTime dateAndTimeInNewYork  = ZonedDateTime.of(localtDateAndTime, america );
        System.out.println("现在的日期和时间在特定的时区 : " + dateAndTimeInNewYork);

        ////使用 YearMonth类处理特定的日期
        YearMonth currentYearMonth = YearMonth.now();
        System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());

        YearMonth creditCardExpiry = YearMonth.of(2028, Month.FEBRUARY);
        System.out.printf("Your credit card expires on %s %n", creditCardExpiry);

        //检查闰年
        if(today.isLeapYear()){
            System.out.println("This year is Leap year");
        }else {
            System.out.println("2018 is not a Leap year");
        }

        //计算两个日期之间的天数和月数
        today = LocalDate.now();
        LocalDate java8Release = LocalDate.of(2018, Month.MAY, 14);
        Period periodToNextJavaRelease = Period.between(java8Release,today);
        System.out.println("Months left between today and Java 8 release : year " + periodToNextJavaRelease.getYears()+", month:"
                + periodToNextJavaRelease.getMonths()+", days :"+periodToNextJavaRelease.getDays());



        //获取时间戳
        Instant timestamp = Instant.now();
        System.out.println("What is value of this instant " + timestamp.toEpochMilli());

        // 使用预定义的格式化工具去解析或格式化日期
        String dayAfterTommorrow = "20180210";
        LocalDate formatted = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.printf("Date generated from String %s is %s %n", dayAfterTommorrow, formatted);

        //2022-10-08 -- 10年8号
        LocalDate tDate = LocalDate.parse("2022-10-08");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("MM月dd日");
        String sDate = df.format(tDate);
        System.out.println(sDate);

        System.out.println(LocalDate.parse("2022-05-10").format(DateTimeFormatter.ofPattern("MM月dd号")));


    }



}
